<template>
  <a-modal
    :title="title"
    :width="720"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="模板名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            v-decorator="['name', { rules: [{ required: true, min: 2, message: '请输入至少2个字符' }] }]"
          />
        </a-form-item>
        <a-form-item label="初始价格" :labelCol="labelCol" :wrapperCol="wrapperCol" help="当场地使用该模板时，给场地所有时段设置的初始价格">
          <a-input-number
            :min="0"
            v-decorator="['price', { initialValue: 100, rules: [{ required: true, message: '请输入至少1个数字' }] }]"
          />
        </a-form-item>
        <a-form-item label="初始状态" :labelCol="labelCol" :wrapperCol="wrapperCol" help="当场地使用该模板时，给场地所有时段设置的初始状态">
          <a-select
            v-decorator="['status', { rules: [{ required: true, message: '请选择一个初始状态!' }] }]"
            placeholder="请选择一个初始状态"
          >
            <a-select-option :value="0"> 空闲 </a-select-option>
            <a-select-option :value="1"> 已预约 </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="数字越小越靠前">
          <a-input-number
            :min="0"
            v-decorator="['sort', { initialValue: 10, rules: [{ required: true, message: '请输入至少1个数字' }] }]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import _ from 'lodash'
import * as Api from '@/api/time/template'

export default {
  props: {},
  data() {
    return {
      // 对话框标题
      title: '',
      // 标签布局属性
      labelCol: {
        span: 7,
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13,
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录
      record: {}
    }
  },
  methods: {
    /**
     * 显示对话框
     */
    edit(record) {
      this.title = '编辑时间段模板'
      this.visible = true

      this.record = record
      // 设置默认值
      this.setFieldsValue()
    },

    /**
     * 设置默认值
     */
    setFieldsValue () {
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        const data = _.pick(this.record, ['name', 'price', 'status', 'sort'])
        setFieldsValue(data)
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit(e) {
      e.preventDefault()
      const {
        form: { validateFields },
      } = this
      // 表单验证
      validateFields((errors, values) => {
        !errors && this.onFormSubmit(values)
      })
    },

    /**
    * 提交到后端api
    */
    onFormSubmit (values) {
      this.confirmLoading = true
      Api.edit({ time_template_id: this.record['time_template_id'], ...values })
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel() {
      this.visible = false
      this.form.resetFields()
    },
  },
}
</script>
